Dino Geek, try to help you

How to configure Virtual Hosts with `.htaccess`?


Certainly! Configuring Virtual Hosts with `.htaccess` involves several steps to ensure that multiple domains or subdomains can be hosted on a single server. Here, I’ll provide a comprehensive explanation, examples, and sources.

  1. Overview

A Virtual Host in Apache allows you to run multiple websites on the same web server. You can configure different domain names or IP addresses to point to different directories on your server. Virtual Hosts can be set up in the main Apache configuration file (usually `httpd.conf` or `apache2.conf`), but they can also be managed using `.htaccess` files for more granular control.

  1. Prerequisites

1. Apache Web Server: Make sure you have Apache installed and running.
2. Enable `mod_rewrite`: Ensure that the `mod_rewrite` module is enabled.

You can check if `mod_rewrite` is enabled by running:

```
sudo a2enmod rewrite
sudo systemctl restart apache2
```

  1. Step-by-Step Configuration

  1. Step 1: Edit Apache Configuration File

Open your Apache configuration file, usually found at `/etc/apache2/sites-available/000-default.conf` (Ubuntu/Debian) or `/etc/httpd/conf/httpd.conf` (CentOS/Fedora).

Add the following configuration:

```
ServerAdmin webmaster@yourdomain.com DocumentRoot “/var/www/yourdomain“ ServerName yourdomain.com ServerAlias www.yourdomain.com

AllowOverride All ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined ```

  1. Step 2: Create Directories and Permissions

Create the directory structure for your websites:

```
sudo mkdir -p /var/www/yourdomain
sudo chown -R $USER:$USER /var/www/yourdomain
sudo chmod -R 755 /var/www/yourdomain
```

  1. Step 3: Configure `.htaccess`

Next, create an `.htaccess` file in your website’s root directory. This file will allow you to manage URL rewrites, permissions, and other settings.

```
touch /var/www/yourdomain/.htaccess
```

Add the following content to your `.htaccess` file as an example:

```

  1. Enable Rewrite Engine
    RewriteEngine On

  1. Example: Redirect www to non-www
    RewriteCond %{HTTP_HOST} ^www\.(.)$ [NC]
    RewriteRule ^(.
    )$ http://%1/$1 [R=301,L]
  1. Example: Custom 404 Page
    ErrorDocument 404 /404.html
  1. Deny access to .htaccess file
    Order Allow,Deny Deny from all

    ```

  1. Restart Apache

After making these changes, be sure to restart Apache to apply the configurations:

```
sudo systemctl restart apache2 # Ubuntu/Debian
sudo systemctl restart httpd # CentOS/Fedora
```

  1. Testing

To test your configuration, open a web browser and navigate to your domain. If everything is set up correctly, you should see your website. To test the `.htaccess` rules, try visiting a URL that doesn’t exist to ensure the custom 404 page is displayed.

  1. Sources

1. Apache HTTP Server Documentation:
- Official documentation for Apache Virtual Hosts: https://httpd.apache.org/docs/2.4/vhosts/
- Official documentation for `.htaccess`: https://httpd.apache.org/docs/2.4/howto/htaccess.html

1. DigitalOcean Community Tutorials:
- A beginner’s guide to using `.htaccess` files for Apache: https://www.digitalocean.com/community/tutorials/how-to-use-the-htaccess-file

1. Apache2 Ubuntu Documentation:
- Setting up Virtual Hosts in Ubuntu: https://help.ubuntu.com/community/ApacheMySQLPHP

By following the steps outlined above, you should be able to configure and manage Virtual Hosts using `.htaccess` effectively. This allows you to have better control over multiple domains hosted on a single server.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use